<template>
  <view class="content">
    <view :style="{ height: `${statusBarHeight}px` }"></view>
    <u-navbar
      :custom-back="customBack"
      back-text="返回"
      title="离线模式"
    ></u-navbar>
    <view>
      <TreeCollapseVue v-if="treeData" :treeData="treeData"></TreeCollapseVue>
    </view>
  </view>
</template>

<script>
import TreeCollapseVue from "../../components/luckysheet/TreeCollapse.vue";
export default {
  components: {
    TreeCollapseVue,
  },
  data() {
    return {
      statusBarHeight: this.$statusBarHeight,
      treeData: null,
      directories: {
        index: 1,
      },
      flagArr: [],
    };
  },
  onLoad() {
    this.loadFiles();
  },
  methods: {
    arrayToTree() {
      let root = plus.io.convertLocalFileSystemURL("_downloads/template");
      let tempDirs = JSON.parse(JSON.stringify(this.directories));
      tempDirs = Object.values(tempDirs);
      const result = [];
      const map = {};
      for (const item of tempDirs) {
        if (
          typeof item !== "object" ||
          item.name == ".WeDrive" ||
          (item.type == "file" && !item.name.endsWith(".xlsx"))
        )
          continue;
        map[item.fullPath] = { ...item };
        let tempPath = root + "/" + item.name;
        if (tempPath === item.fullPath) {
          result.push(map[item.fullPath]); // 根节点
        } else {
          // 其他路径
          let otherFullPath = item.fullPath;
          let baseFullPath = otherFullPath.split("/").slice(0, -1).join("/");
          if (!map[baseFullPath]) {
            map[baseFullPath] = { children: [] }; // 没有父节点， 就创建父节点
          }
          if (!map[baseFullPath].children) {
            map[baseFullPath].children = [];
          }
          map[baseFullPath].children.push(map[item.fullPath]);
        }
      }
      return result;
    },
    fileLoaded() {
      let treeData = {};

      treeData = this.arrayToTree();
      this.treeData = treeData;
    },
    loadFiles(dir = "_downloads/template") {
      this.flagArr.push(dir);
      let self = this;
      plus.io.resolveLocalFileSystemURL(
        dir, //指定的目录
        function (entry) {
          var directoryReader = entry.createReader(); //获取读取目录对象
          directoryReader.readEntries(
            function (entries) {
              //历遍子目录即可
              for (var i = 0; i < entries.length; i++) {
                let fullPath = entries[i].fullPath;
                let type = "";
                let suffix = fullPath.split(".");
                suffix = suffix[suffix.length - 1];
                let obj = {
                  fullPath: fullPath,
                  name: entries[i].name,
                };
                if (entries[i].isDirectory) {
                  type = "dir";
                  obj.fullpath = fullPath;
                  self.loadFiles(dir + "/" + obj.name);
                } else {
                  type = "file";
                }
                obj.type = type;
                obj.suffix = suffix;
                obj.id = self.directories.index++;
                self.directories[fullPath] = obj;
              }
              let index = self.flagArr.findIndex((a) => a == dir);
              self.flagArr.splice(index, 1);
              if (self.flagArr.length == 0) {
                self.fileLoaded();
              }
            },
            function (err) {
              console.log("访问目录失败");
              self.flagArr.length = 0;
              self.directories = {
                index: 1,
              };
              self.fileLoaded();
            }
          );
        },
        function (err) {
          console.log("访问指定目录失败:" + err.message);
        }
      );
    },
    customBack() {
      uni.navigateTo({
        url: "/pages/system/login/index",
      });
    },
  },
};
</script>

<style></style>
